package com.rech.xbexpress3;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.rech.dialog.CustomDialog;
import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

/**
 * 菜鸟裹裹下单信息填写页
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class Jj3Activity extends AppCompatActivity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;
    private int mBoxType = 0;
    private String ljm;
    private String sjr;
    private String kdysj;
    private String urlImage;
    String boxValue;
    private TextView djs ;
    private Timer countTimer;
    EditText kdysj_ctrl;
    EditText ljm_ctrl1;
    EditText ljm_ctrl2;
    //EditText sjr_ctrl;
    private String resCur;
    private Vector<Pair<String,String> > vec;
    ImageView jj3_cx_img;
    ImageView jj3_x_img;
    ImageView jj3_z_img;
    ImageView jj3_d_img;
    ImageView jj3_cd_img;
    private int jjcount = 300;
    private Button tv_cabinet ;
    private Button tv_package ;
    private LinearLayout boxtype_group;
    private LinearLayout packagetype_group;
    private CustomDialog commitDialog;
    private Button btn_confirm;
    private long jj3_clicktime = 0;

    public static final long TIME_INTERVAL = 2000L;
    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;
    private final Handler mHideHandler = new Handler();

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar


        }
    };
    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
        }
    };
    private boolean mVisible;
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };
    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };
    private boolean isBox;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation","");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(R.layout.activity_jj3_land);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.activity_jj3);
        }

        //eventbus
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        vec = new Vector<Pair<String,String>>(1, 1);
        mVisible = true;getSupportActionBar().hide();
        djs = findViewById(R.id.jj3_djs);
        //boxValue  =  sharedPreferences.getString("jj_boxvalue", "[{}]");
        //final int expType     =  sharedPreferences.getInt("jj_exp",0);
        final int expType = 2; //菜鸟裹裹为2

        boxtype_group = (LinearLayout) findViewById(R.id.boxtype_group);
        packagetype_group = (LinearLayout) findViewById(R.id.packagetype_group);
        tv_cabinet = findViewById(R.id.tv_cabinet);
        tv_cabinet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_package.setBackgroundResource(R.drawable.bg_gray_right_5);
                tv_cabinet.setBackgroundResource(R.drawable.bg_blue_left_5);
                tv_cabinet.setTextColor(getResources().getColor(R.color.yellow));
                tv_package.setTextColor(getResources().getColor(R.color.white));
                boxtype_group.setVisibility(View.VISIBLE);
                packagetype_group.setVisibility(View.INVISIBLE);
            }
        });
        tv_package = findViewById(R.id.tv_package);
        tv_package.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_package.setBackgroundResource(R.drawable.bg_blue_right_5);
                tv_cabinet.setBackgroundResource(R.drawable.bg_gray_left_5);
                tv_cabinet.setTextColor(getResources().getColor(R.color.white));
                tv_package.setTextColor(getResources().getColor(R.color.yellow));
                boxtype_group.setVisibility(View.INVISIBLE);
                packagetype_group.setVisibility(View.VISIBLE);
            }
        });

        kdysj_ctrl = findViewById(R.id.jj3_kdysj);
        ljm_ctrl1 = findViewById(R.id.jj3_ljm_1);
        //ljm_ctrl2 = findViewById(R.id.jj3_ljm_2);
        //sjr_ctrl = findViewById(R.id.jj3_sjr);

        /*ljm_ctrl1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().trim().length() == 4 && ljm_ctrl1.isFocused()) {
                    ljm_ctrl2.setFocusable(true);
                    ljm_ctrl2.requestFocus();
                }
            }
        });*/


        kdysj_ctrl.setText(sharedPreferences.getString("CabinetType_cai_phone",""));
        ljm_ctrl1.setText(sharedPreferences.getString("jj_ljm1",""));
        //ljm_ctrl2.setText(sharedPreferences.getString("jj_ljm2",""));
        //sjr_ctrl.setText(sharedPreferences.getString("jj_sjr",""));


        //1为快递100 2为菜鸟裹裹有揽件码
        ImageView jj3_image = (ImageView) findViewById(R.id.jj3_image);
        /*if(expType == 2){
            urlImage     =  sharedPreferences.getString("CabinetType_cai","");
            findViewById(R.id.step1).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
            findViewById(R.id.step2).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 0));
            //findViewById(R.id.jj3_qr_text).setVisibility(View.INVISIBLE);
        }
        else{
            urlImage     =  sharedPreferences.getString("CabinetType_kuai","");

            findViewById(R.id.step1).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 0));
            findViewById(R.id.step2).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
        }*/

        urlImage = sharedPreferences.getString("CabinetType_cai","");
        new FetchUrlImageTask(urlImage,jj3_image).execute();

        TextView jj3_qx = (TextView) findViewById(R.id.tv_back);
        jj3_qx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long nowTime = System.currentTimeMillis();

                if (nowTime - jj3_clicktime > TIME_INTERVAL) {
                    jj3_clicktime = nowTime;
                    SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();

                    /*editor.putString("jj_kdysj",kdysj);
                    editor.putString("jj_ljm",ljm);
                    editor.putString("jj_sjr",sjr);*/
                    editor.putString("jj_kdysj","");
                    editor.putString("jj_ljm1","");
                    editor.putString("jj_ljm2","");
                    //editor.putString("jj_sjr","");
                    editor.apply();
                    Intent intent = new Intent();
                    setResult(1001, intent);
                    Exit();
                    finish();
                }
            }
        });



        Button jj3_qd = (Button) findViewById(R.id.jj3_qd);
        jj3_qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long nowTime = System.currentTimeMillis();

                if (nowTime - jj3_clicktime > TIME_INTERVAL) {
                    jj3_clicktime = nowTime;

                    //String jj3_ljm_str = ljm_ctrl1.getText().toString().trim() + "-" + ljm_ctrl2.getText().toString().trim();
                    String jj3_ljm_str = ljm_ctrl1.getText().toString().trim();
                    /*EditText jj3_sjr = (EditText) findViewById(R.id.jj3_sjr);
                    String jj3_sjr_str = jj3_sjr.getText().toString();*/
                    EditText jj3_kdysj = (EditText) findViewById(R.id.jj3_kdysj);
                    String jj3_kdysj_str = jj3_kdysj.getText().toString();
                    if(jj3_kdysj_str.length() < 1)
                    {
                        Toast.makeText(Jj3Activity.this, "请输入快递员手机", Toast.LENGTH_SHORT).show();
                        return ;
                    }

                    if(expType == 2){
                        if(jj3_ljm_str.length() < 4)
                        {
                            Toast.makeText(Jj3Activity.this, "请输入正确的揽件码", Toast.LENGTH_SHORT).show();
                            return ;
                        }
                    }

                    if(mBoxType == 0) {
                        //Toast.makeText(Jj3Activity.this, "请选择格子类型", Toast.LENGTH_SHORT).show();
                        showNoTypeDialog();
                        return ;
                    }


                    ljm = jj3_ljm_str;
                    //sjr = jj3_sjr_str;
                    kdysj = jj3_kdysj_str;

                    SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                    editor.putString("jj_kdysj",kdysj);
                    editor.putString("jj_ljm1",ljm_ctrl1.getText().toString().trim());
                    //editor.putString("jj_ljm2",ljm_ctrl2.getText().toString().trim());
                    //editor.putString("jj_sjr",sjr);
                    editor.apply();

                    //如果是重选格子，则不需要再次进行人脸识别
                    SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
                    boolean isReChoose = sharedPreferences.getBoolean("isReChoose",false);
                    if (isReChoose) {
                        new FetchIO485GridTaskForJJ(mBoxType).execute();
                    } else {
                        Intent intent = new Intent();
                        intent.putExtra("mBoxType", mBoxType);
                        setResult(1008, intent);
                        Exit();
                        finish();
                    }


                    //新型柜子不论什么类型都需要同时开大门
                    /*QueueService.getInstance().sendNotify("900300fa1aa0");
                    OtherUtil.OpenVoice7(Jj3Activity.this);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    new FetchIO485GridTaskForJJ(mBoxType).execute();
                                }
                            }, 1000);
                        }
                    });*/

                    //如果是袋子类型的，需要同时开大门
                    /*if (!isBox) {

                    }*/
                }

            }
        });
        countTimer=new Timer();
        countTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                jjcount--;
                Message message = new Message();
                message.what=20000;
                EventBus.getDefault().post(message);
                if(jjcount < 1){
                    SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();

                    /*editor.putString("jj_kdysj",kdysj);
                    editor.putString("jj_ljm",ljm);
                    editor.putString("jj_sjr",sjr);*/
                    editor.putString("jj_kdysj","");
                    editor.putString("jj_ljm1","");
                    //editor.putString("jj_ljm2","");
                    //editor.putString("jj_sjr","");
                    editor.apply();
                    //OtherUtil.restartApplication(Jj3Activity.this);
                    Exit();
                    finish();
                }
            }
        },0,1000);

        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);
    }

    private void showNoTypeDialog() {
        View view = View.inflate(this, R.layout.dialog_no_type, null);
        initDialogView(view);
        commitDialog = new CustomDialog(this, OtherUtil.dp2px(500), OtherUtil.dp2px(560),
                view, R.style.CustomDialog);
        commitDialog.show();
    }

    private void initDialogView(View view) {
        btn_confirm = view.findViewById(R.id.btn_confirm);

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeDialog();
            }
        });
    }

    private void closeDialog() {
        if (commitDialog != null && commitDialog.isShowing()) {
            commitDialog.dismiss();
        }
    }

    private void Exit(){
        countTimer.cancel();
        EventBus.getDefault().unregister(this);

        //清除重选格子标志位
        SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
        editor.putBoolean("isReChoose",false);
        editor.apply();
    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
//        mHideHandler.removeCallbacks(mShowPart2Runnable);
//        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
//        mHideHandler.removeCallbacks(mHidePart2Runnable);
//        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    /**
     * Schedules a call to hide() in delay milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
    private void handleBoxType(Object obj){
        try{
            JSONObject result = new JSONObject(obj.toString());
            if(result.getString("code").equals("100")) {
                JSONArray ordersJson = result.getJSONArray("data");
                boxValue = ordersJson.toString();
                JSONArray orders = new JSONArray(boxValue);
                boxValue = orders.toString();
                boolean hasBox = false;
                for (int i = 0; i < orders.length(); i++) {
                    JSONObject jsonObject1 = orders.getJSONObject(i);
                    String itemType = jsonObject1.get("type").toString();
                    if(itemType.equals("1")) {
                        hasBox = true;

                        boxtype_group.setVisibility(View.VISIBLE);
                        String itemId = jsonObject1.get("id").toString();
                        String itemName = jsonObject1.get("name").toString();
                        final int count = Integer.parseInt(jsonObject1.get("count").toString());

                        final String nameId = "jj3_name"+String.valueOf(i+1);
                        final String layoutId = "jj3_box"+String.valueOf(i+1);
                        ((TextView)findViewById(OtherUtil.getResId(nameId,R.id.class))).setText(itemName + "\n" + count);
                        vec.add(new Pair<String, String>(itemId, nameId));
                        ((LinearLayout) findViewById(OtherUtil.getResId(layoutId, R.id.class))).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (count == 0) {
                                    return;
                                }
                                ((TextView) findViewById(OtherUtil.getResId(nameId, R.id.class))).setBackgroundResource(R.drawable.img_bg_select);
                                ((TextView) findViewById(OtherUtil.getResId(nameId, R.id.class))).setTextColor(getResources().getColor(R.color.yellow));
                                resCur = nameId;
                                ChoiceBox();
                            }
                        });
                        findViewById(OtherUtil.getResId(layoutId,R.id.class)).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
                    }else{
                        String itemId = jsonObject1.get("id").toString();
                        String itemName = jsonObject1.get("name").toString();
                        final int count = Integer.parseInt(jsonObject1.get("count").toString());

                        final String nameId = "package_name"+String.valueOf(i+1);
                        final String layoutId = "jj3_package"+String.valueOf(i+1);
                        ((TextView)findViewById(OtherUtil.getResId(nameId,R.id.class))).setText(itemName + "\n" + count);
                        vec.add(new Pair<String, String>(itemId,nameId));
                        ((LinearLayout)findViewById(OtherUtil.getResId(layoutId,R.id.class))).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (count == 0) {
                                    return;
                                }
                                ((TextView)findViewById(OtherUtil.getResId(nameId,R.id.class))).setBackgroundResource(R.drawable.img_bg_select);
                                ((TextView) findViewById(OtherUtil.getResId(nameId, R.id.class))).setTextColor(getResources().getColor(R.color.yellow));
                                resCur = nameId;
                                ChoiceBox();
                            }
                        });
                        findViewById(OtherUtil.getResId(layoutId,R.id.class)).setLayoutParams(new LinearLayout.LayoutParams(LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.MATCH_PARENT, 1));
                    }

                    if (!hasBox) {
                        packagetype_group.setVisibility(View.VISIBLE);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){

            case 10108:
                handleOpenGridTask(message.obj);
                break;
            case 10109:
                Toast.makeText(Jj3Activity.this, "开柜失败", Toast.LENGTH_SHORT).show();
                break;
            case 10120:
                handleGetIO485Task(message.obj);
                break;
            case 10121:
                Toast.makeText(Jj3Activity.this, "获取柜子状态失败", Toast.LENGTH_SHORT).show();
                break;
            case 20000:
                handleDJS();
                break;
            case 20101:
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                /*editor.putString("jj_kdysj",kdysj);
                editor.putString("jj_ljm1",ljm_ctrl1.getText().toString().trim());
                editor.putString("jj_ljm2",ljm_ctrl2.getText().toString().trim());
                //editor.putString("jj_sjr",sjr);
                editor.apply();
                setResult(1005, intent);
                Exit();
                finish();*/

                editor.putBoolean("isReChoose",false);
                editor.apply();
                intent.putExtra("isFromWX", false);
                setResult(1005, intent);
                Exit();
                finish();
                break;
            case 10106:
                handleBoxType(message.obj);
                break;
        }
    }
    public void handleOpenGridTask(Object obj) {
        try{
            JSONObject result = new JSONObject(obj.toString());
            if(result.getString("code").equals("100")){
                //String jj_gridtype = result.getJSONObject("data").getString("gridtype");
                String jj_gridnum = result.getJSONObject("data").getString("gridNum");
                String jj_io = result.getJSONObject("data").getString("IO");
                String jj_485 = result.getJSONObject("data").getString("485");
                String jj_cabinetgridid = result.getJSONObject("data").getString("cabinetGridId");
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();

                editor.putString("jj_kdysj",kdysj);
                editor.putString("jj_ljm1",ljm_ctrl1.getText().toString().trim());
                //editor.putString("jj_ljm2",ljm_ctrl2.getText().toString().trim());
                //editor.putString("jj_sjr",sjr);
                editor.putString("jj_cabinetgridid",jj_cabinetgridid);
                //editor.putString("jj_gridtype",jj_gridtype);
                editor.putString("jj_gridnum",jj_gridnum);
                editor.putString("jj_io",jj_io);
                editor.putString("jj_485",jj_485);
                editor.apply();
                OtherUtil.OpenVoice2(this);
                Intent intent = new Intent();
                setResult(1005, intent);
                Exit();
                finish();
            }
        }
        catch (Exception e){
            Log.d("handleSendGridTask",e.toString());
        }
    }
    private void handleGetIO485Task(Object obj){
        try{
            JSONObject result = new JSONObject(obj.toString());
            if(result.getString("code").equals("100")){
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();

                editor.putString("reopen",obj.toString());
                editor.putString("jj_gridnum",result.getJSONObject("data").getString("gridNum"));
                editor.putString("jj_cabinetgridid",result.getJSONObject("data").getString("cabinetGridId"));
                editor.apply();

                new FetchOpenLocalGridTaskv2ForJJ(obj.toString()).execute();

            }
        }
        catch (Exception e){
            Log.d("handleSendGridTask",e.toString());
        }
    }
    private void handleDJS(){
        djs.setText(String.valueOf(jjcount));
    }

    private void ChoiceBox(){
        for(int i=0;i<vec.size();i++){
            String layoutId = vec.get(i).second;
            if( layoutId.equals(resCur)){
                mBoxType = Integer.parseInt(vec.get(i).first);
                //((LinearLayout)findViewById(OtherUtil.getResId(layoutId,R.id.class))).setBackgroundColor(getResources().getColor(R.color.button_red));

            }else{
                ((TextView)findViewById(OtherUtil.getResId(layoutId,R.id.class))).setBackgroundResource(R.drawable.bg_blue_button_5);
                ((TextView) findViewById(OtherUtil.getResId(layoutId, R.id.class))).setTextColor(getResources().getColor(R.color.white));
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new FetchGetBoxStateTask().execute();
    }
}
